
cc.Class({
    extends: cc.Component,

    properties: {
        audioSource: {
            type: cc.AudioSource,
            default: null,
        },
        // music name
        label: {
            type: cc.Label,
            default: null,
        },
        // process
        processSlider: {
            type: cc.Slider,
            default: null,
        },
        // volume
        volSlider: {
            type: cc.Slider,
            default: null,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.label.string = '--/--';
        this.processSlider.progress = 0;
        this.volSlider.progress = 0.5;
    },

    update() {
      const audio = this.audioSource;
      this.label.string = audio.getCurrentTime().toFixed(1)+' s/'+audio.getDuration().toFixed(1)+' s';
      this.processSlider.progress = audio.getCurrentTime()/audio.getDuration();
      audio.volume = this.volSlider.progress;
    },

    play() {
        this.audioSource.play();
    },

    pause() {
      this.audioSource.pause();
    },

    stop() {
      this.audioSource.stop();
    },

    resume() {
      this.audioSource.resume();
    },

    // change volume
    changeVol(slider) {
      this.audioSource.volume = slider.progress;
    },

    // change audio process
    didSliderSlided(slider) {
        const t = slider.progress * this.audioSource.getDuration();
        this.audioSource.setCurrentTime(t);
    },
});
